/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package puzzleevolution;
        import java.awt.Canvas;
        import java.awt.Color;
        import java.awt.Dimension;
        import java.awt.Frame;
        import java.awt.Graphics;
        import java.awt.event.WindowAdapter;
        import java.awt.event.WindowEvent;
        import java.awt.image.BufferedImage;
        import java.awt.image.WritableRaster;

    /**
     * Trida predstavujici vykreslovani zadaneho jedince reseni
     *
     * @author vorispe1
     */
    public class ResultPicture extends Frame {

        static BufferedImage I = new BufferedImage(Config.rozmeryObrazkuX, Config.rozmeryObrazkuY, BufferedImage.TYPE_INT_RGB);
        static WritableRaster wr;
        static Frame f;
        
        /**
         * Metoda vykresli jeden puzzle dil na zadane pozici v reseni
         *
         * @param puzzlePiece
         * @param coordinateX
         * @param coordinateY
         */
        static public void drawPiece(PuzzlePiece puzzlePiece, int coordinateX, int coordinateY){
            wr = I.getRaster();
            Pixel[][] pixels = puzzlePiece.getPieceImage();

            for(int i=0;i<pixels.length;i++){
                for(int j=0;j<pixels[0].length;j++){
                    int[] pixel = new int[3];
                    pixel[0] = pixels[i][j].getRed();
                    pixel[1] = pixels[i][j].getGreen();
                    pixel[2] = pixels[i][j].getBlue();
                    wr.setPixel(coordinateX+i, coordinateY+j, pixel);
                }
            }
            f.add("Center", new MainCanvas());
            f.setSize(new Dimension(Config.rozmeryObrazkuX + 50, Config.rozmeryObrazkuY + 75));
            f.setVisible(true);
       }


        /**
         * Metoda vykresli cele reseni jedince
         *
         * @param puzzlePieces
         */
        static public void drawAll(PuzzlePiece[][] puzzlePieces){
            f = new Frame( "Obrazek" );
            f.addWindowListener(new WindowAdapter(){
            @Override
                public void windowClosing(WindowEvent we){
                    System.exit(0);
                };
            });
            
            for (int i=0; i < Config.pocetDilkuX; i++){
                for (int j=0; j < Config.pocetDilkuY; j++){
                    drawPiece(puzzlePieces[j][i], j * Config.delkaDilku, i * Config.delkaDilku);
                }
            }
        }
    }

    class MainCanvas extends Canvas
    {
        @Override
        public void paint(Graphics g)
        {
            g.drawImage(ResultPicture.I, 0, 0, Color.red, null);
            //Dimension s = getSize();
            //g.drawOval(0, 0, s.width, s.height);
        }
    }
